var pageii = null;
$(function () {
    forminit();
    $("#addCheck").click("click", function () {
        addCheck();
    });
    $("#setChange").click("click", function () {
        setChange();
    });
    $("#addBrightStop").click("click", function () {
        addBrightStop();
    });
    $("#deleteCheck").click("click", function () {
        deleteCheck();
    });
    $("#isReturn").click("click", function () {
        isReturn();
        // window.location="/taskManage/isReturn";
    });
    $("#isClose").click("click", function () {
        isClose();
    });
    $("#changeSubmit").click("click", function () {
        changeSubmit();
    });
    $("#inspectList").click("click", function () {

        window.location = "/taskManage/checkItem?taskType=" + $("#taskType").val() + "&isSpot=1" + "&taskId=" + $("#taskId").val()
    });
    $("#brightSpotList").click("click", function () {

        window.location = "/taskManage/checkItem?taskType=" + $("#taskType").val() + "&isSpot=0" + "&taskId=" + $("#taskId").val()
    });
    getProjectName();
});
function setChange() {
    var flag = false;
    $.each($('#dynamic-table input[type=checkbox]'), function (i, n) {
        flag = true;
        return;
    });
    if (flag) {
        pageii = layer.open({
            title: "整改单",
            type: 2,
            fixed: true,
            //shadeClose : true,
            area: ["80%", "60%"],
            content: "/taskManage/setChangeUI?taskId=" + $("#taskId").val()
        });
    }
}
function checkDetails(checkId) {

    pageii = layer.open({
        title: "详情",
        type: 2,
        fixed: true,
        //shadeClose : true,
        area: ["750px", "80%"],
        content: "/taskManage/checkDetailsUI?checkId=" + checkId + "&taskType=" + $("#taskType").val()
    });
}
function scoreCheck(checkId, checkItemId) {

    pageii = layer.open({
        title: "评分",
        type: 2,
        fixed: true,
        //shadeClose : true,
        area: ["600px", "70%"],
        content: "/taskManage/scoreCheckUI?checkId=" + checkId + "&checkItemId=" + checkItemId + "&taskType=" + $("#taskType").val()
    });
}
function deleteCheck() {
    var checkIds = '';
    $.each($('#dynamic-table input[type=checkbox]:checked'), function (i, n) {
        checkIds += $(this).attr('data-id') + ',';
    });
    if (checkIds == '') {
        layer.msg('请至少选择一条数据!');
        return;
    }
    checkIds = checkIds.substring(0, checkIds.length - 1);
    layer.confirm('确定删除?', function (index) {
        layer.close(index);
        $.ajax({
            url: '/taskManage/deleteCheck',
            data: {checkIds: checkIds},
            dataType: "text",
            type: 'post',
            success: function (data) {
                if (data == 'success') {
                    layer.msg('删除成功');
                    location.replace(location)
                    // location.href = '/taskManage/checkItem?taskType=' + $("#taskType").val() + '&taskId=' + $("#taskId").val();
                }
            },
            error: function (data) {
                layer.msg('删除失败');
            }
        });
    });
}
function addCheck() {
    top.pageii = top.layer.open({
        title: "添加检查项",
        type: 2,
        fixed: true,
        //shadeClose : true,
        area: ["800px", "100%"],
        content: "/taskManage/addCheckUI?taskId=" + $("#taskId").val()
    });
}
function addBrightStop() {
    pageii = layer.open({
        title: "添加亮点",
        type: 2,
        fixed: true,
        //shadeClose : true,
        area: ["80%", "60%"],
        content: "/taskManage/addBrightStopUI?taskId=" + $("#taskId").val()
    });
}
function getProjectName() {
    var dataUrl ="/modules";
    var url = /*$("#dataUrl").val()*/   dataUrl  + "/biz/project/findProjectById?id=" + $("#projectId").val();
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            $("#projectName").text(result.caption);
        }
    });
}
function isReturn() {
    pageii = layer.open({
        title: "打回意见",
        type: 2,
        fixed: true,
        //shadeClose : true,
        area: ["80%", "30%"],
        content: "/taskManage/isReturn?taskId=" + $("#taskId").val()
    });
}
//整改
function changeOption(checkId) {
  /*  var url = $("#dataUrl").val() + "/recorder/checkDetail?checkId=" + checkId
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        aynsc: false,
        success: function (data) {
            $("#closeMemo").val(null == data.closeMemo ? '' : data.closeMemo);
            $("#changeId").val(checkId);
            /!*if (null != data.reformFileRecord&&data.reformFileRecord.length>0) {
             for(var i=0;i<data.reformFileRecord.length;i++){
             if(i>2){
             break;
             }
             var url=$("#qnUrl").val() + data.reformFileRecord[i].oldPhotoPath;
             $("#oldImg"+i).attr("src", url);
             $("#oldImg"+i).show();
             $("#oldImg"+i).click("click", function () {
             enlargePhoto(url);
             });
             }
             $("#imgFile0").hide();
             $("#imgFile2").hide();
             $("#imgFile3").hide();
             $("#submitBtn").hide();
             }*!/
        }
    });*/
    pageii = layer.open({
        title: "整改",
        type: 2,
        fixed: true,
        //shadeClose : true,
        area: ["80%", "50%"],
        content: "/taskManage/changeOptionUI?checkId=" +checkId
    });
  /*  pageii = layer.open({
        title: "整改",
        type: 1,
        fixed: true,
        //shadeClose : true,
        area: ["80%", "30%"],
        content: $("#changeOption"),
        end: function () {
            $("#closeMemo").val('');
            $("#changeId").val('');
            /!*   $("#imgFile0").show();
             $("#imgFile2").show();
             $("#imgFile3").show();*!/
            $("#imgFile").val('');
            $("#imgFile2").val('');
            $("#imgFile3").val('');
            /!*   $("#oldImg0").remove('src');
             $("#oldImg0").hide();
             $("#oldImg1").remove('src');
             $("#oldImg1").hide();
             $("#oldImg2").remove('src');
             $("#oldImg2").hide();
             $("#submitBtn").show();*!/
        }
    });*/
}
function forminit() {
    $("form").validate({
        submitHandler: function (form) {// 必须写在验证前面，否则无法ajax提交
            if (!checkForm()) {
                return;
            }
            $(form).ajaxSubmit({
                dataType: "text",
                success: function (data) {
                    if (data == "success") {
                        // layer.confirm('添加成功!是否关闭窗口?', function (index) {
                            layer.close(pageii);
                            // layer.close(index);
                            return false;
                        // });
                    } else {
                        layer.alert('添加失败！', 3);
                    }
                }
            });
        },
    });
}
function checkForm() {
    if ($("#closeMemo").val() == '') {
        layer.msg('请填写整改意见');
        return false;
    }
    if ($("#imgFile").val() == '' && $("#imgFile2").val() == '' && $("#imgFile3").val() == '') {
        layer.msg('请上传图片');
        return false;
    }
    return true;
}
//整改提交
function changeSubmit() {
    var num=0;
    $(".closeMemo").each(function () {
        var closeMemo = $(this).val();
        // alert(checkItemRuleId);
        if (closeMemo == "") {
            num++;
            return false;
        }
    });
    if(num>0){
        layer.msg("请全部整改完成再提交");
        return;
    }
    layer.confirm('是否确定整改提交?', function (index) {
        layer.close(index);
        var url = "/taskManage/changeSubmit?taskId=" + $("#taskId").val();
        $.ajax({
            url: url,
            method: "post",
            dataType: "text",
            aynsc: true,
            success: function (data) {
                if (data == "success") {
                    layer.msg('整改提交成功！');
                    setTimeout(function () {
                        location.href = '/taskManage/rectificationTask';
                    }, 1000);
                } else {
                    layer.alert('整改提交失败！', 3);
                }
            },
            error: function (data) {
                layer.alert('整改提交失败！', 3);
            }
        });
    });
}
//闭环
function isClose() {
    layer.confirm('是否确定闭环?', function (index) {
        layer.close(index);
        var url = "/taskManage/taskClose?taskId=" + $("#taskId").val();
        $.ajax({
            url: url,
            method: "post",
            dataType: "text",
            aynsc: true,
            success: function (data) {
                if (data == "success") {
                    layer.msg('闭环成功！');
                    setTimeout(function () {
                        location.href = '/taskManage/checkItem?taskType=3&taskId=' + $("#taskId").val();
                    }, 1000);
                } else {
                    layer.alert('闭环失败！', 3);
                }
            },
            error: function (data) {
                layer.alert('闭环失败！', 3);
            }
        });
    });
}

function enlargePhoto(url) {
    if(url.src.indexOf( '/images/whitepanel_bg2.png')>-1){
        return
    }
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        shadeClose: true,
        area: ['500px','auto'], //宽高
        content: "<img  src=" + url.src + " width='90%' style='width: 500px;' />"
    });
}